﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain
{
    public class ASMSDbContext : DbContext
    {
        static ASMSDbContext()
        {
            Database.SetInitializer<ASMSDbContext>(null);
        }

        public ASMSDbContext()
            : base("name=sunboxsoft-asms-db")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Entity.EAsset>().ToTable("ASMS_Asset");
            modelBuilder.Entity<Entity.EAsset>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EBusiness>().ToTable("ASMS_Business");
            modelBuilder.Entity<Entity.EBusiness>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMasterData>().ToTable("ASMS_MasterData");
            modelBuilder.Entity<Entity.EMasterData>().HasKey(t => t.Code);
            modelBuilder.Entity<Entity.EMasterDataItem>().ToTable("ASMS_MasterDataItem");
            modelBuilder.Entity<Entity.EMasterDataItem>().HasKey(t => t.Code);
            modelBuilder.Entity<Entity.EStaff>().ToTable("ASMS_Staff");
            modelBuilder.Entity<Entity.EStaff>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStaffPosition>().ToTable("ASMS_StaffPosition");
            modelBuilder.Entity<Entity.EStaffPosition>().HasKey(t => new { t.StaffId, t.StationId, t.Post });
            modelBuilder.Entity<Entity.ECustomer>().ToTable("ASMS_Customer");
            modelBuilder.Entity<Entity.ECustomer>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EAutomobile>().ToTable("ASMS_Automobile");
            modelBuilder.Entity<Entity.EAutomobile>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EBookingBill>().ToTable("ASMS_BookingBill");
            modelBuilder.Entity<Entity.EBookingBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EBookingService>().ToTable("ASMS_BookingService");
            modelBuilder.Entity<Entity.EBookingService>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESeed>().ToTable("ASMS_Seed");
            modelBuilder.Entity<Entity.ESeed>().HasKey(t => t.Key);
            modelBuilder.Entity<Entity.EStation>().ToTable("ASMS_Station");
            modelBuilder.Entity<Entity.EStation>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EServiceCategory>().ToTable("ASMS_ServiceCategory");
            modelBuilder.Entity<Entity.EServiceCategory>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EServiceItem>().ToTable("ASMS_ServiceItem");
            modelBuilder.Entity<Entity.EServiceItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaintenanceBill>().ToTable("ASMS_MaintenanceBill");
            modelBuilder.Entity<Entity.EMaintenanceBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaintenanceBillMaterial>().ToTable("ASMS_MaintenanceBillMaterial");
            modelBuilder.Entity<Entity.EMaintenanceBillMaterial>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaintenanceBillPayItem>().ToTable("ASMS_MaintenanceBillPayItem");
            modelBuilder.Entity<Entity.EMaintenanceBillPayItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaintenanceBillServiceItem>().ToTable("ASMS_MaintenanceBillServiceItem");
            modelBuilder.Entity<Entity.EMaintenanceBillServiceItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPayItem>().ToTable("ASMS_PayItem");
            modelBuilder.Entity<Entity.EPayItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaterial>().ToTable("ASMS_Material");
            modelBuilder.Entity<Entity.EMaterial>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EWarehouse>().ToTable("ASMS_Warehouse");
            modelBuilder.Entity<Entity.EWarehouse>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStock>().ToTable("ASMS_Stock");
            modelBuilder.Entity<Entity.EStock>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockChangedRecord>().ToTable("ASMS_StockChangedRecord");
            modelBuilder.Entity<Entity.EStockChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockIncreaseBill>().ToTable("ASMS_StockIncreaseBill");
            modelBuilder.Entity<Entity.EStockIncreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockIncreaseBillItem>().ToTable("ASMS_StockIncreaseBillItem");
            modelBuilder.Entity<Entity.EStockIncreaseBillItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESupplier>().ToTable("ASMS_Supplier");
            modelBuilder.Entity<Entity.ESupplier>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EWorkingBill>().ToTable("ASMS_WorkingBill");
            modelBuilder.Entity<Entity.EWorkingBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaterialRequisitionBill>().ToTable("ASMS_MaterialRequisitionBill");
            modelBuilder.Entity<Entity.EMaterialRequisitionBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackage>().ToTable("ASMS_Package");
            modelBuilder.Entity<Entity.EPackage>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESalesPrice>().ToTable("ASMS_SalesPrice");
            modelBuilder.Entity<Entity.ESalesPrice>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackageItem>().ToTable("ASMS_PackageItem");
            modelBuilder.Entity<Entity.EPackageItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackageScope>().ToTable("ASMS_PackageScope");
            modelBuilder.Entity<Entity.EPackageScope>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackagePurchaseBill>().ToTable("ASMS_PackagePurchaseBill");
            modelBuilder.Entity<Entity.EPackagePurchaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackageInstance>().ToTable("ASMS_PackageInstance");
            modelBuilder.Entity<Entity.EPackageInstance>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackageInstanceItem>().ToTable("ASMS_PackageInstanceItem");
            modelBuilder.Entity<Entity.EPackageInstanceItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPayRecord>().ToTable("ASMS_PayRecord");
            modelBuilder.Entity<Entity.EPayRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ERechargeBill>().ToTable("ASMS_RechargeBill");
            modelBuilder.Entity<Entity.ERechargeBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ERechargeItem>().ToTable("ASMS_RechargeItem");
            modelBuilder.Entity<Entity.ERechargeItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBill>().ToTable("ASMS_SettlementBill");
            modelBuilder.Entity<Entity.ESettlementBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBillCoupon>().ToTable("ASMS_SettlementBillCoupon");
            modelBuilder.Entity<Entity.ESettlementBillCoupon>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBillMaterial>().ToTable("ASMS_SettlementBillMaterial");
            modelBuilder.Entity<Entity.ESettlementBillMaterial>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBillPackageItem>().ToTable("ASMS_SettlementBillPackageItem");
            modelBuilder.Entity<Entity.ESettlementBillPackageItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBillPayItem>().ToTable("ASMS_SettlementBillPayItem");
            modelBuilder.Entity<Entity.ESettlementBillPayItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ESettlementBillServiceItem>().ToTable("ASMS_SettlementBillServiceItem");
            modelBuilder.Entity<Entity.ESettlementBillServiceItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockDecreaseBill>().ToTable("ASMS_StockDecreaseBill");
            modelBuilder.Entity<Entity.EStockDecreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockDecreaseBillItem>().ToTable("ASMS_StockDecreaseBillItem");
            modelBuilder.Entity<Entity.EStockDecreaseBillItem>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EStockThresholdValue>().ToTable("ASMS_StockThresholdValue");
            modelBuilder.Entity<Entity.EStockThresholdValue>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ECapitalIncreaseBill>().ToTable("ASMS_CapitalIncreaseBill");
            modelBuilder.Entity<Entity.ECapitalIncreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ECapitalDecreaseBill>().ToTable("ASMS_CapitalDecreaseBill");
            modelBuilder.Entity<Entity.ECapitalDecreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.ECapitalChangedRecord>().ToTable("ASMS_CapitalChangedRecord");
            modelBuilder.Entity<Entity.ECapitalChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EPackageInstanceItemChangedRecord>().ToTable("ASMS_PackageInstanceItemChangedRecord");
            modelBuilder.Entity<Entity.EPackageInstanceItemChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EBillHeader>().ToTable("ASMS_BillHeader");
            modelBuilder.Entity<Entity.EBillHeader>().HasKey(t => new { t.BillId, t.BillType });
            modelBuilder.Entity<Entity.EMaterialRetain>().ToTable("ASMS_MaterialRetain");
            modelBuilder.Entity<Entity.EMaterialRetain>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EServiceItemRetain>().ToTable("ASMS_ServiceItemRetain");
            modelBuilder.Entity<Entity.EServiceItemRetain>().HasKey(t => t.Id);
            modelBuilder.Entity<Entity.EMaterialReturnedBill>().ToTable("ASMS_MaterialReturnedBill");
            modelBuilder.Entity<Entity.EMaterialReturnedBill>().HasKey(t => t.Id);


            // 视图
            modelBuilder.Entity<View.VBookingBill>().ToTable("ASMS_V_BookingBill");
            modelBuilder.Entity<View.VBookingBill>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VBookingService>().ToTable("ASMS_V_BookingService");
            modelBuilder.Entity<View.VBookingService>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VBusiness>().ToTable("ASMS_V_Business");
            modelBuilder.Entity<View.VBusiness>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VAutomobile>().ToTable("ASMS_V_Automobile");
            modelBuilder.Entity<View.VAutomobile>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VCustomer>().ToTable("ASMS_V_Customer");
            modelBuilder.Entity<View.VCustomer>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VStaffPosition>().ToTable("ASMS_V_StaffPosition");
            modelBuilder.Entity<View.VStaffPosition>().HasKey(p => new { p.StaffId, p.StationId, p.Post });
            modelBuilder.Entity<View.VMaterial>().ToTable("ASMS_V_Material");
            modelBuilder.Entity<View.VMaterial>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VSupplier>().ToTable("ASMS_V_Supplier");
            modelBuilder.Entity<View.VSupplier>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VMaintenanceBill>().ToTable("ASMS_V_MaintenanceBill");
            modelBuilder.Entity<View.VMaintenanceBill>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VMaintenanceBillServiceItem>().ToTable("ASMS_V_MaintenanceBillServiceItem");
            modelBuilder.Entity<View.VMaintenanceBillServiceItem>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VMaintenanceBillMaterial>().ToTable("ASMS_V_MaintenanceBillMaterial");
            modelBuilder.Entity<View.VMaintenanceBillMaterial>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VMaintenanceBillPayItem>().ToTable("ASMS_V_MaintenanceBillPayItem");
            modelBuilder.Entity<View.VMaintenanceBillPayItem>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VStation>().ToTable("ASMS_V_Station");
            modelBuilder.Entity<View.VStation>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VPackagePurchaseBill>().ToTable("ASMS_V_PackagePurchaseBill");
            modelBuilder.Entity<View.VPackagePurchaseBill>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VPackageInstance>().ToTable("ASMS_V_PackageInstance");
            modelBuilder.Entity<View.VPackageInstance>().HasKey(p => p.Id);
            modelBuilder.Entity<View.VPackageInstanceItem>().ToTable("ASMS_V_PackageInstanceItem");
            modelBuilder.Entity<View.VPackageInstanceItem>().HasKey(p => new { p.PackageInstanceId, p.PackageItemId });
            modelBuilder.Entity<View.VPackageInstanceItemChangedRecord>().ToTable("ASMS_V_PackageInstanceItemChangedRecord");
            modelBuilder.Entity<View.VPackageInstanceItemChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VRechargeBill>().ToTable("ASMS_V_RechargeBill");
            modelBuilder.Entity<View.VRechargeBill>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSettlementBillMaterial>().ToTable("ASMS_V_SettlementBillMaterial");
            modelBuilder.Entity<View.VSettlementBillMaterial>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSettlementBillPackageItem>().ToTable("ASMS_V_SettlementBillPackageItem");
            modelBuilder.Entity<View.VSettlementBillPackageItem>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSettlementBillPayItem>().ToTable("ASMS_V_SettlementBillPayItem");
            modelBuilder.Entity<View.VSettlementBillPayItem>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSettlementBillServiceItem>().ToTable("ASMS_V_SettlementBillServiceItem");
            modelBuilder.Entity<View.VSettlementBillServiceItem>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VStockChangedRecord>().ToTable("ASMS_V_StockChangedRecord");
            modelBuilder.Entity<View.VStockChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VStockThresholdValue>().ToTable("ASMS_V_StockThresholdValue");
            modelBuilder.Entity<View.VStockThresholdValue>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VCapitalIncreaseBill>().ToTable("ASMS_V_CapitalIncreaseBill");
            modelBuilder.Entity<View.VCapitalIncreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VCapitalDecreaseBill>().ToTable("ASMS_V_CapitalDecreaseBill");
            modelBuilder.Entity<View.VCapitalDecreaseBill>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VCapitalChangedRecord>().ToTable("ASMS_V_CapitalChangedRecord");
            modelBuilder.Entity<View.VCapitalChangedRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSettlementBill>().ToTable("ASMS_V_SettlementBill");
            modelBuilder.Entity<View.VSettlementBill>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VPayRecord>().ToTable("ASMS_V_PayRecord");
            modelBuilder.Entity<View.VPayRecord>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VSalesPrice>().ToTable("ASMS_V_SalesPrice");
            modelBuilder.Entity<View.VSalesPrice>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VStockMaterial>().ToTable("ASMS_V_StockMaterial");
            modelBuilder.Entity<View.VStockMaterial>().HasKey(t => t.Id);
            modelBuilder.Entity<View.VMaterialWithStock>().ToTable("ASMS_V_MaterialWithStock");
            modelBuilder.Entity<View.VMaterialWithStock>().HasKey(t => new { t.Id, t.WarehouseId });

            base.OnModelCreating(modelBuilder);
        }
    }
}
